<?php
class content_commonModel extends baseModel{
	protected $table = 'content';

    //访问计数
    public function views_content($aid,$views){
        $data['views'] = $views + 1;
        $condition['aid'] = $aid;
        $this->update($condition,$data);
    }

    //上一篇
    public function prev_content($aid,$cid,$ext_id){
        if(!empty($ext_id)){
            $model_info=api('expand_model','model_info',$ext_id);
            $expand="LEFT JOIN {$this->model->pre}expand_content_{$model_info['table']} C ON C.aid = A.aid";
            $expand_field="C.*,";
        }
        $info="
            SELECT {$expand_field}A.*
             FROM {$this->model->pre}content A 
             LEFT JOIN {$this->model->pre}category B ON B.cid = A.cid
             {$expand}
             WHERE A.aid<{$aid} AND A.status=1 AND B.cid={$cid} ORDER BY A.aid desc LIMIT 1
            ";
            $info=$this->model->query($info);
        return $info[0]; 
    }

    //下一篇
    public function next_content($aid,$cid,$ext_id){
        if(!empty($ext_id)){
            $model_info=api('expand_model','model_info',$ext_id);
            $expand="LEFT JOIN {$this->model->pre}expand_content_{$model_info['table']} C ON C.aid = A.aid";
            $expand_field="C.*,";
        }
        $info="
            SELECT {$expand_field}A.*
             FROM {$this->model->pre}content A 
             LEFT JOIN {$this->model->pre}category B ON B.cid = A.cid
             {$expand}
             WHERE A.aid>{$aid} AND A.status=1 AND B.cid={$cid} ORDER BY A.aid asc LIMIT 1
            ";
            $info=$this->model->query($info);
        return $info[0];
    }

    //替换后内容
    public function replace_content($content){
        $replace = $this->model->table('replace')->select();
        $content=html_out($content);
        if (!empty($replace)) {
            foreach ($replace as $export) {
                if(empty($export['key'])){
                    if(empty($export['num'])){
                        $export['num']=1;
                    }
                    $content = preg_replace("/(?!<[^>]+)".preg_quote($export['key'],'/')."(?![^<]*>)/",$export['content'], $content,$export['num']);
                }
            }
        }
        return $content;
    }

    //增加TAG链接
    public function tag_link($content,$aid){
        $info="
            SELECT A.*
             FROM {$this->model->pre}tags A 
             LEFT JOIN {$this->model->pre}tags_relation B ON B.tid=A.id
             WHERE B.aid={$aid}
        ";
        $taglist = $this->model->query($info);
        $content=html_out($content);
        if (!empty($taglist)) {
            foreach ($taglist as $export) {
                if(!empty($export['name'])){
                    $content = preg_replace("/(?!<[^>]+)".preg_quote($export['name'],'/')."(?![^<]*>)/", '<a href="'.url('tags/list/index',array('tag'=>$export['name'])).'"  target="_blank">'.$export['name'].'</a>',$content,1);
                }
            }
        }
        return $content;
    }

}